#ifndef _IOTGO_H_
#define _IOTGO_H_

#include <stdio.h>
#include <stdint.h>

/* some error code here to indicate the fail*/
/* To be defined further */
#define ERROR_CODE             xxx /*something gets wrong*/
#define ERROR_CREATE_THREAD     -1 /* create thread failed */
#define ERROR_CREATE_DATA_POOL  -2 /* create data pool failed */
#define ERROR_MUTIRUN           -3 /* iotgo service has been running*/
#define ERROR_CONNECTION_NOT_READY -4 /* iotgo connection is not ready*/

/* end of error code */


/*
 * @name : configWithApp
 * @desc : This function exchange info with APP.\n
 *         First it sets up a wifi AP.\n
 *         Then it sets up TCP server.\n
 *         Then exchange info under IOTgo protocol.\n
 *         Send to   APP : deviceID & APIkey\n
 *         Read from APP : Wifi SSID & password\n
 *                         Distribute Server domain & port\n
 *         The read info will store in the device.\n
 * @param : over time number(seconds).
 * @return : numbers for error. To be defined.\n
 *           0 if everything is ok & info has been stored.
 */   
extern int32_t configWithApp(uint8_t overtime);


/*
 * @name : iotgoConnect
 * @desc : Set up connection between device & iotgo server.\n
 *         Then keep the connection under iotgo protocol.\n
 *         It will endlessly run if possible.\n
 *         It will keep reading from the long server  
 *         and store user_data into a iotgo_buf.\n
 *         If something gets wrong & connection can not be kept,
 *         it will free resources & return a number for the error.
 * @param : 
 * @return : numbers for error. To be defined.
 */   
extern int32_t iotgoConnect(void);


/*
 * @name : iotgoRead
 * @desc : Read from the iotgo data buffer.\n
 *         The data has been parsed under IOTgo protocol.\n
 *         This function will not keep waiting to read.\n
 *         This function is like socket read() without blocking.\n 
 *         This function can be called anytime but not always run seccessfully.
 * @param : fd , it's reserved now, maybe can be used one day for multi servers.\n
 *          buf , a pointer to a buffer for caller to store data.\n
 *          nbytes, numbers for data to be read.\n
 * @return : numbers that read successfully or error code.\n
 *           There's one ERROR code returned to show that the connection has not been ready.\n
 */   
extern int32_t iotgoRead(uint32_t fd,void *buf, size_t nbytes);

/*
 * @name : iotgoWrite
 * @desc : Write data to the iotgo server.\n
 *         The data will be packaged under IOTgo protocol.\n
 *         This function is like socket write().\n 
 *         This function can be called anytime but not always run seccessfully.
 * @param : fd , it's reserved now, maybe can be used one day for multi servers.\n
 *          buf , data in this buf will be packaged and sent to the server.\n
 *          nbytes, numbers for data to be sent.\n
 * @return : numbers that sent successfully or error code.\n
 *           There's one ERROR code returned to show that the connection has not been ready.\n
 */   
extern int32_t iotgoWrite(uint32_t fd,void *buf, size_t nbytes);

/*
 * @name : iotgoUpdateP2Ppassword
 * @desc : update P2P IDs & password to the iotgo server.\n
 *         The data will be packaged under IOTgo protocol.\n
 *         This function is like socket write().\n 
 *         This function can be called anytime but not always run seccessfully.
 * @param : mechineID , for different mechines. It's a pointer to a 31-bit string. \n
 *          p2pID , the p2p entry id. It's a pointer to a 31-bit string. \n
 *          password , It's a pointer to a 31-bit string.\n
 * @return : numbers that sent successfully or error code.\n
 *           There's one ERROR code returned to show that the connection has not been ready.\n
 */   
extern int32_t iotgoUpdateP2Ppassword(uint8_t *mechineID, uint8_t *p2pID, uint8_t *password);

/*
 * @name : iotgoUpdateWifiRssi
 * @desc : update wifi rssi to the iotgo server.\n
 *         The data will be packaged under IOTgo protocol.\n
 *         This function is like socket write().\n 
 *         This function can be called anytime but not always run seccessfully.\n
 *         This function should be called periodically or whenever RSSI changes 3dB or more.
 * @param : rssi , the num of Received Signal Strength Indication
 * @return : 0 if it succeeds, -1 if it fails.
 */   
extern int8_t iotgoUpdateWifiRssi(uint8_t rssi);

/*
 * @name : iotgoUpdateCloudStoreDone
 * @desc : update the result of remote-store operation.\n
 *         The data will be packaged under IOTgo protocol.\n
 *         This function is like socket write().\n 
 *         This function can be called anytime but not always run successfully. 
 *         This function should be called after Cloud-Store-Operation is successfully done.
 * @param : A point of a sturct stored the cloud-store information.\n
 *          struct result{
 *                  unsigned char bucketName[24]; 
 *                  unsigned char filePath[128]; 
 *                  unsigned int  fileSize;     
 *                  unsigned char storeType;    // 0 -> aws , 1 -> QiNiu 
 *                  unsigned char sign[32];     // sha256(deviceid + ts + apikey)
 *	    }
 *          Just copy the value to the element of the struct in this function. 
 *          Do Not change the pointer.
 * @return : 0 if it succeeds, -1 if it fails.
 */   
extern int8_t iotgoUpdateCloudStoreDone(void *pstruct);

/*
 * @name : iotgoUpdateStatusLed
 * @desc : update the state of status-led to the iotgo server.\n
 *         The data will be packaged under IOTgo protocol.\n
 *         This function is like socket write().\n 
 *         This function can be called anytime but not always run seccessfully.\n 
 *         This function should be called whenever status-led is changed by local.
 * @param : led_status , 0 == led_on\n
 *                       1 == led_off.
 * @return : 0 if it succeeds, -1 if it fails.
 */   
extern int8_t iotgoUpdateStatusLed(uint8_t led_status);

/*
 * @name : iotgoUpdateOtaDone
 * @desc : update the result of OTA to the iotgo server.\n
 *         The data will be packaged under IOTgo protocol.\n
 *         This function is like socket write().\n 
 *         This function can be called anytime but not always run seccessfully.
 * @param : sequence, a 14-bit string.  This sequence is same with the one in function   
 *          setOtaInfo(unsigned char *pinfo ,unsigned char *sequence)
 * @return : 0 if it succeeds, -1 if it fails
 */   
extern int8_t iotgoUpdateOtaDone(uint8_t *sequence);

/*
 * @name : iotgoUpdateStorageInfo
 * @desc : update local storage information to the iotgo server.\n
 *         The data will be packaged under IOTgo protocol.\n
 *         This function is like socket write().\n 
 *         This function can be called anytime but not always run seccessfully.\n 
 *         This function should be called whenever remain storage changes 10MB.
 * @param : A point of a sturct stored the parameters.\n
 *          struct storage{
 *	        unsigned int total;   // total 4000 == 4000MB == 4G
 *              unsigned int remain;  // 2000 == 2000MB == 2G remain to be used.
 *	    }
 *          Just copy the value to the element of the struct in this function. 
 *          Do Not change the pointer.
 * @return : 0 if it succeeds, -1 if it fails.
 */   
extern int8_t iotgoUpdateStorageInfo(void *pstruct);

/*
 * @name : iotgoUpdateMotionAlert
 * @desc : update alert information to the iotgo server.\n
 *         The data will be packaged under IOTgo protocol.\n
 *         This function is like socket write().\n 
 *         This function can be called anytime but not always run seccessfully.
 * @param : A point of a sturct stored the alert information.\n
 *          struct {
 *	            	unsigned int alert_num;   //  
 *		    		unsigned int time; 
 *                  unsigned int type;			
 *	    } 
 *          Just copy the value to the element of the struct in this function. 
 *          Do Not change the pointer. 
 * @return : 0 if it succeeds, -1 if it fails
 */   
extern int8_t iotgoUpdateMotionAlert(void *pstruct);

/*
 * @name : iotgoGetUserApiKey
 * @desc : This function is to copy user apikey to the input array.
 *         An APIkey is a 37-bit string.(include the '\0')\n
 *         For example "174f460f-430f-4dda-9064-de3cff77c189".\n
 *         This function is to COPY the device info to where
 *         the parameter pointers points to.\n
 *         Each parameter is a pointer to an array.\n
 *         Do not change the pointer itself.
 * @param : A pointer to an array storing userApiKey.
 *          This function just copy the value to the input array.
 *          Do Not change the pointer. 
 * @return : 0 if it succeeds, -1 if it fails
 */   
extern int8_t iotgoGetUserApiKey(uint8_t *user_apikey);

#endif
